import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
//静态加载 初始化看到的页面和框架
import Login from '@/views/login'
import Layout from '@/layout/Layout.vue'

//隐藏原地跳转错误
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

//配置路由地址 和 页面组件的一一对应关系
const routes = []

//路由一分为三

//静态路由 所有人都能访问的路由 不需要权限的路由
export const constantRoutes = [
  /* 首页重定向 */
  {
    path: '/',
    redirect: '/home',
    hidden: true,
  },
  /* 登录 */
  {
    path: '/login',
    component: Login,
    hidden: true,
  },
  /* 首页 */
  {
    path: '/home',
    component: Layout, //框架
    meta: {path: '/home', title: '后台首页', icon: 'home'},
    children: [
      {
        path: '',
        component: () => import('@/views/home'),
      },
    ],
  },
]

//动态路由(异步路由) 需要划分权限的路由
export const asnycRoutes = [
  /* 订单管理 */
  {
    path: '/order',
    component: Layout,
    //roles字段 被数组包含的角色才能够访问
    //如果一级不能访问 则里面的都不能被访问
    meta: {path: '/order', title: '订单管理', roles: ['super'], icon: 'order'},
    children: [
      {
        path: '',
        component: () => import('@/views/order'),
      },
      {
        hidden: true,
        path: '/order/order-detail',
        component: () => import('@/views/order/order-detail'),
      },
      {
        hidden: true,
        path: '/order/order-edit/:id',
        component: () => import('@/views/order/order-edit'),
      },
    ],
  },
  /* 商品管理 */
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/goods-list',
    //如果所有人都能访问 就不需要roles: ['super']
    meta: {path: '/goods', title: '商品管理', icon: 'goods'},

    children: [
      {
        meta: {path: '/goods/goods-list', title: '商品列表'},
        path: '/goods/goods-list', //不写斜杠相当于/goods/goods-list
        component: () => import('@/views/goods/goods-list'),
      },
      {
        meta: {path: '/goods/goods-add', title: '商品添加', roles: ['super']},
        path: '/goods/goods-add',
        component: () => import('@/views/goods/goods-add'),
      },
      {
        meta: {path: '/goods/goods-cate', title: '商品分类'},
        path: '/goods/goods-cate',
        component: () => import('@/views/goods/goods-cate'),
      },
    ],
  },
  /* 店铺管理 */
  {
    path: '/shop',
    component: Layout,
    meta: {path: '/shop', title: '店铺管理', roles: ['super'], icon: 'shop'},
    children: [
      {
        path: '',
        component: () => import('@/views/shop'),
      },
    ],
  },
  /* 账号管理 */
  {
    path: '/account',
    component: Layout,
    redirect: '/account/account-list',
    meta: {path: '/account', title: '账号管理', icon: 'user'},
    children: [
      {
        meta: {
          path: '/account/account-list',
          title: '账号列表',
          roles: ['super'],
        },
        path: '/account/account-list',
        component: () => import('@/views/account/account-list'),
      },
      {
        meta: {
          path: '/account/account-add',
          title: '账号添加',
          roles: ['super'],
        },
        path: '/account/account-add',
        component: () => import('@/views/account/account-add'),
      },
      {
        meta: {path: '/account/password', title: '修改密码'},
        path: '/account/password',
        component: () => import('@/views/account/password-modify'),
      },
      {
        meta: {path: '/account/account-person', title: '个人中心'},
        path: '/account/account-person',
        component: () => import('@/views/account/account-person'),
      },
    ],
  },
  /* 销售统计 */
  {
    path: '/total',
    component: Layout,
    meta: {path: '/total', title: '销售统计', roles: ['super'], icon: 'total'},
    redirect: '/total/total-goods',
    children: [
      {
        meta: {path: '/total/total-goods', title: '商品统计'},
        path: '/total/total-goods',
        component: () => import('@/views/total/total-goods'),
      },
      {
        meta: {path: '/total/total-order', title: '订单统计'},
        path: '/total/total-order',
        component: () => import('@/views/total/total-order'),
      },
    ],
  },
]

//错误路由 404相关
export const errorRoutes = [
  /* 错误路由 */
  {
    path: '*',
    redirect: '/404',
  },
  {
    path: '/404',
    component: () => import('@/views/404/error404.vue'),
  },
]

const router = new VueRouter({
  //初始化加载静态路由
  routes: constantRoutes,
})

export default router
